"""Handles RPCs for background tasks. Chosen instead of webhooks with taskqueue
because of its lightweight. And because it is easier to setup."""

from google.appengine.api.datastore import Key
from google.appengine.ext import db

from util.constants import DEV_TYPE_NAME_ANDROID as ANDROID
from util.constants import DEV_TYPE_NAME_CHROME as CHROME
from util.constants import DEV_TYPE_NAME_BB as BB
import util, entities

def delete(data_key):
	"""Delete the data with specified key from datastore"""
	def delete_data_txn():
		db.delete(data_key)
	
	db.run_in_transaction(delete_data_txn)
	
def notify_user_of_new_data(owner_device_key_name):
	"""Checks to see if the devices whose key is passed supports push
notifications. If it does, notify the device of new data."""

	device = entities.Device.get_by_key_name(owner_device_key_name)
	
	notify_method_str = 'notify_%s' % device.device_type
	notify_method = None
	
	if hasattr(util, notify_method_str):
		notify_method = getattr(util, notify_method_str)
	else:
		#TODO Log
		return
		
	notify_method(owner_device_key_name)
